<?php

namespace App\Admin\Controllers;

use App\Models\Base\LogEverydayData;
use App\Service\ChannelService;
use App\Service\ServerService;
use Encore\Admin\Controllers\ModelForm;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Grid;
use Encore\Admin\Layout\Content;

class StatdataController extends Controller
{
    use ModelForm;

    /**
     * Index interface.
     *
     * @return Content
     */
    public function index()
    {
        return Admin::content(function (Content $content) {
            $content->header(trans('game.data_statistic'));
            $content->description(trans('game.server_all_base'));
            $content->body($this->grid());
        });
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Admin::grid(LogEverydayData::class, function (Grid $grid) {
            $get = request()->all();
            $type_id = getFindType($get);
            $grid->model()->where('type', $type_id)->orderBy('id', 'desc');
            $grid->id();
            $grid->date_time(trans('game.type'))->sortable();
            if (!in_array($type_id, [1, 3])) {
                $grid->Platform()->name(trans('game.where_the_platform'));
                $grid->channel()->name(trans('game.channel'));
            }
            if(in_array($type_id,[3,4])){
                $grid->server()->name(trans('game.server_name'));
            }
            $grid->new_player_num(trans('game.new_user'));
            $grid->new_role_num(trans('game.new_role'));
            $grid->login_player_num(trans('game.login_user'));
            $grid->amount(trans('game.amount'));
            $grid->pay_player_num(trans('game.pay_user_number'));
            $grid->pay_player_ratio(trans('game.pay_rate'))->display(function ($ratio) {
                return $ratio;
            });
            $grid->new_player_pay_num(trans('game.pay_new_user_no'));
            $grid->new_player_amount(trans('game.new_user_amount'));
            $grid->new_player_ltv(trans('game.new_user_one_day_ltv'));
            $grid->old_player_pay_num(trans('game.pay_old_user_no'));
            $grid->old_player_amount(trans('game.old_user_amount'));
            $grid->online_max_num(trans('game.max_online'));
            $grid->online_min_num(trans('game.min_online'));
            $grid->online_avg_num(trans('game.avg_online'));
            $grid->arpu();
            $grid->arppu();
            $grid->disableCreateButton();
            $grid->disableActions();
            $grid->tools(function ($tools) {
                $tools->batch(function ($batch) {
                    $batch->disableDelete();
                });
            });

            $grid->filter(function ($filter) {
                $filter->disableIdFilter();
                $params = request()->all();
                $filter->equal('platform_id', trans('game.where_the_platform'))->select(ChannelService::getPlatformList())
                    ->load('channel_id', route('order.channel'), 'id', 'name');
                $filter->equal('channel_id', trans('game.channel'))->select();
                $filter->equal('server_id', trans('game.server_name'))->select(ServerService::getServerList());
                $filter->between('date_time', trans('game.date'))->datetime();
                $fileter_data = [];
                if (isset($params['platform_id']) && '-1' == $params['platform_id']) {
                    $fileter_data[] = 'platform_id';
                }
                if (isset($params['channel_id']) && '-1' == $params['channel_id']) {
                    $fileter_data[] = 'channel_id';
                }
                if (isset($params['server_id']) && '-1' == $params['server_id']) {
                    $fileter_data[] = 'server_id';
                }
                $filter->setNotWhere($fileter_data);
            });
        });
    }
}
